package indi.zhifa.recipe.bailan.busy.controller.api;

import com.alibaba.fastjson.JSON;
import indi.zhifa.recipe.bailan.busy.entity.dto.PlayerDto;
import indi.zhifa.recipe.bailan.busy.entity.enums.EWeapon;
import indi.zhifa.recipe.bailan.busy.entity.po.PlayerEntity;
import indi.zhifa.recipe.bailan.busy.service.IPlayerService;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import indi.zhifa.recipe.bailan.framework.common.entity.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "2.用户接口")
@RequestMapping("/api/user")
@RequiredArgsConstructor
@ZfRestController
public class PlayerApi {
    private final IPlayerService mPlayerService;

    @Operation(summary = "创建用户")
    @PostMapping("")
    PlayerEntity create(@RequestBody PlayerDto pPlayerDto){
        PlayerEntity playerEntity = mPlayerService.create(pPlayerDto);
        return playerEntity;
    }

    @Operation(summary = "修改用户")
    @PutMapping("/{id}")
    PlayerEntity edit(@PathVariable("id") @Parameter(description = "用户Id") Long pId,
                      @RequestBody PlayerDto pPlayerDto){
        PlayerEntity playerEntity = mPlayerService.edit(pId,pPlayerDto);
        log.info("{}修改为{}",pId, JSON.toJSONString(pPlayerDto));
        return playerEntity;
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    RestResponse<String> delete(@PathVariable("id") @Parameter(description = "用户Id") Long pId){
        if(mPlayerService.delete(pId)){
            return RestResponse.ok("删除成功");
        }
        return RestResponse.ok("删除失败");
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/{id}")
    PlayerEntity info(@PathVariable("id") @Parameter(description = "用户Id") Long pId){
        PlayerEntity playerEntity = mPlayerService.info(pId);
        return playerEntity;
    }

    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    List<PlayerEntity> list(@RequestParam(name = "name",required = false) @Parameter(description = "用户名") String pName,
                            @RequestParam(name = "weapon",required = false) @Parameter(description = "武器") EWeapon pWeapon){
        List<PlayerEntity> playerEntityList = mPlayerService.list(pName,pWeapon);
        return playerEntityList;
    }
}
